﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace SisLegal.Model
{
    public class Lawyer
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; } 	
        public string Fono { get; set; } 	
        public string Address { get; set; } 

        public Lawyer()
	    {
            this.ID = 0;
            this.Name = "";
            this.Email = "";
            this.Fono = "";
            this.Address = "";
	    }
    }

    public class LawyerManager
    {
        public List<Lawyer> GetAll()
        {
            List<Lawyer> result = new List<Lawyer>();
            string sqlQuery = String.Format("SELECT * FROM Lawyer ORDER BY ID");
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            Lawyer process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new Lawyer();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    process.Email = dataReader["Email"].ToString();
                    process.Fono = dataReader["Fono"].ToString();
                    process.Address = dataReader["Address"].ToString();
                    result.Add(process);
                }
            }
            return result;
        }
        public Int32 Count()
        {
            List<TypeProcess> result = new List<TypeProcess>();
            string sqlQuery = String.Format("  SELECT COUNT(ID) AS total FROM Lawyer");
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            command.CommandType = CommandType.Text;
            Int32 countStart = (Int32)command.ExecuteScalar();
            return countStart;
        }

        public List<Lawyer> Paginate(Int32 pag)
        {
            List<Lawyer> result = new List<Lawyer>();
            string sqlQuery = String.Format("SELECT * FROM (" +
                "SELECT ROW_NUMBER() OVER(ORDER BY ID) AS NUMBER, * FROM Lawyer " +
                ")as tbl " +
                "WHERE NUMBER BETWEEN((" + Convert.ToString(pag) + "-1) * (10 +1)) AND (" + Convert.ToString(pag) + " * 10) ORDER BY ID");

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            Lawyer process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new Lawyer();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    process.Email = dataReader["Email"].ToString();
                    process.Fono = dataReader["Fono"].ToString();
                    process.Address = dataReader["Address"].ToString();
                    result.Add(process);
                }
            }
            return result;
        }

        public int Save(Lawyer obj)
        {
            string createQuery = String.Format("Insert into Lawyer (Name, Email, Fono, Address) Values('{0}','{1}','{2}','{3}');"
                + "Select @@Identity", obj.Name, obj.Email, obj.Fono, obj.Address);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = null;

            command = new SqlCommand(createQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
            }
            catch (Exception ex)
            {

            }

            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }

        public int Update(Lawyer obj)
        {
            string updateQuery = String.Format("Update Lawyer SET Name='{0}', Email='{1}', Fono='{2}', Address='{3}' Where ID = {4};", obj.Name, obj.Email, obj.Fono, obj.Address, obj.ID);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = null;
            command = new SqlCommand(updateQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
                else
                {
                    saveID = obj.ID;
                }
            }
            catch (Exception ex)
            {

            }
            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }

        public Lawyer GetById(int articleId)
        {
            Lawyer result = new Lawyer();
            string sqlQuery = String.Format("select * from Lawyer where ID={0}", articleId);
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();

            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    result.ID = Convert.ToInt32(dataReader["ID"]);
                    result.Name = dataReader["Name"].ToString();
                    result.Email = dataReader["Email"].ToString();
                    result.Fono = dataReader["Fono"].ToString();
                    result.Address = dataReader["Address"].ToString();
                }
            }

            return result;
        }

        public bool Delete(int articleId)
        {
            bool result = false;
            string sqlQuery = String.Format("delete from Lawyer where ID = {0}", articleId);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            int rowsDeletedCount = command.ExecuteNonQuery();
            if (rowsDeletedCount != 0)
                result = true;
            command.Dispose();
            connection.Close();
            connection.Dispose();
            return result;
        }
    }
}